<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use User\Api\UserApi;

/**
 * 后台用户控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */

class ActiveController extends AdminController {

//    static protected $allow = array( 'updatePassword','updateNickname','submitPassword','submitNickname');

    /**
     * 考勤日志首页
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function index(){
    	$nickname = I('nickname');
    	$map = array('status'=>array('egt',0));
    	if(isset($nickname)){
    		if(intval($nickname) !== 0){
    			$map['uid'] = intval($nickname);
    		}else{
    			$map['name']  = array('like', '%'.(string)$nickname.'%');
    		}
    	}
        $time_start = I('time-start',0,'strtotime');
        $time_end = I('time-end',0,'strtotime');
        if($time_start>0){
            $map['active_time'][] = array('gt',$time_start);
        }
        if($time_end>0){
            $map['active_time'][] = array('lt',$time_end);
        }
        $list   = $this->lists('active', $map);
        $this->assign('_list', $list);
        $this->meta_title = '签到记录';
        $this->display();
    }

    public function user_detail(){
        $uid = I('uid',0,'intval');
        $obj = M('active'); // 实例化User对象
        $year=date('Y');
        $month=date('m');
        $days=date('t',strtotime("{$year}-{$month}-1"));
        $week=date('w',strtotime("{$year}-{$month}-1"));
        $z = 0;
        $list = $obj->where('uid='.$uid)->order('active_time')->select();

        foreach($list as $key=>$value){
            if($value['active_type']==1){
                $u_date[$value['date_str']][1] = $value['active_time'];
            } else {
                $u_date[$value['date_str']][2] = $value['active_time'];
            }

        }
        for($i=1-$week;$i<=$days;){
            for($j=0;$j<7;$j++){
                if($i>$days || $i<=0){
                    $data[$z][$i]['text'] = '';
                    $data[$z][$i]['j'] = $j;
                }else{
                    $data[$z][$i]['text'] = $i;
                    $data[$z][$i]['j'] = $j;
                    //对应关系
                    if($i<10){
                        $date_str = $year.'-'.$month.'-0'.$i;
                    } else {
                        $date_str = $year.'-'.$month.'-'.$i;
                    }
                    $data[$z][$i]['on'] = $u_date[$date_str][1];
                    $data[$z][$i]['off'] = $u_date[$date_str][2];
                }
                $i++;
            }
            $z++;
        }

        $user_name = M('member')->where('uid='.$uid)->getField('nickname');
//        print_r($user_name);die;
        $this->assign('user_name',$user_name);

        $this->assign('table',$data);
        $this->assign('year',$year);
        $this->assign('month',$month);
        $this->display();
    }

    //导出考勤数据
    public function export_excel(){
        $nickname = I('nickname');
        $map = array('status'=>array('egt',0));
        if(isset($nickname)){
            if(intval($nickname) !== 0){
                $map['uid'] = intval($nickname);
            }else{
                $map['name']  = array('neq', 'admin');
            }
        }
        $time_start = I('time-start',0,'strtotime');
        $time_end = I('time-end',0,'trim');
        $time_end .='23:59:59';
        $time_end = strtotime($time_end);
        if($time_start>0){
            $map['active_time'][] = array('gt',$time_start);
        }
        if($time_end>0){
            $map['active_time'][] = array('lt',$time_end);
        }
        $list = M('active')->where($map)->select();
        $tmp = array();


        vendor("PHPExcel.PHPExcel");
        $obj_php_excel = new \PHPExcel();
        $obj_php_excel->setActiveSheetIndex(0);
        $obj_php_excel->getActiveSheet(0)->setTitle('考勤记录');
        $obj_php_excel->getActiveSheet(0)->setCellValue('A1','姓名')
                                         ->setCellValue('B1','日期')
                                         ->setCellValue('C1','上班时间')
                                         ->setCellValue('D1','下班时间');

        $upA = array();
        $downA = array();
        foreach($list as $key=>$val){
            $key1 = $val['date_str'];

            $key_tmp = $val['uid'].$val['date_str'].$val['active_type'];

            $out['uid'] = $val['uid'];
            $out['date_str'] = $val['date_str'];
            $out['name'] = $val['name'];
            if($val['active_type']==2){
                $downA[$key_tmp] = date('H:i',$val['active_time']);
            }
            if($val['active_type']==1){
                $upA[$key_tmp] = date('H:i',$val['active_time']);
            }
            $tmp[$key1][$val['uid']] = $out;
        }

        $i=2;
//        print_r($tmp);die;
        foreach($tmp as $out_key => $out_val){
            if(is_array($out_val)){
                foreach($out_val as $k=>$v){
                    $obj_php_excel->getActiveSheet(0)->setCellValue('A'.$i,$v['name']);
                    $obj_php_excel->getActiveSheet(0)->setCellValue('B'.$i,$v['date_str']);
                    $key_tmp_str = $v['uid'].$v['date_str'];
                    $obj_php_excel->getActiveSheet(0)->setCellValue('C'.$i,$upA[$key_tmp_str.'1']);
                    $obj_php_excel->getActiveSheet(0)->setCellValue('D'.$i,$downA[$key_tmp_str.'2']);
                    $i++;
                }

            }
        }
        ob_end_clean();//清除缓冲区,避免乱码
        header('pragma:public');
        $xlsTitle = 'export'.date('Y-m-d',$time_start).'~'.date('Y-m-d',$time_end);
        $fileName = 'export'.date('Y-m-d',$time_start).'~'.date('Y-m-d',$time_end);
        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
        header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($obj_php_excel, 'Excel5');
        $objWriter->save('php://output');
        exit;
    }



}